﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DA
{
    public class KhachHang_DA
    {
        QLKhachSanDataContext KHDA ;

        public List<KhachHang> TimKiemKHTheoMa(string tim)
        {
            try
            {
                KHDA = new QLKhachSanDataContext();
                return KHDA.KhachHangs.Where(p => p.MaKH.Contains(tim)).ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                KHDA.Dispose();
            }
        }
        public List<KhachHang> TimKiemKHTheoTen(string tim)
        {
            try
            {
                KHDA = new QLKhachSanDataContext();
                return KHDA.KhachHangs.Where(p => p.TenKH.Contains(tim) || p.TenKH.Contains(tim)).ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                KHDA.Dispose();
            }
        }
        public List<KhachHang> LayDSKhachHang()
        {
            try
            {
                KHDA = new QLKhachSanDataContext();
                return KHDA.KhachHangs.ToList();
            }
            catch (Exception ex)
            {

                throw ex;
            }
            finally
            {
                KHDA.Dispose();
            }
        }
        public bool ThemKhachHang(KhachHang K)
        {
            try
            {
                KHDA = new QLKhachSanDataContext();
                KhachHang s = new KhachHang();
                s.MaKH = K.MaKH;
                s.TenKH = K.TenKH;
                s.GioiTinh = K.GioiTinh;
                s.NgaySinh = K.NgaySinh;
                s.CMND = K.CMND;
                s.NgayTao = K.NgayTao;
                s.DiaChi = K.DiaChi;
                s.LoaiKhach = K.LoaiKhach;
                KHDA.KhachHangs.InsertOnSubmit(s);
                KHDA.SubmitChanges();
                return true;

            }
            catch
            {
                return false;
            }
            finally
            {
                KHDA.Dispose();
            }
        }
        public bool SuaKhachHang(KhachHang K)
        {
            try
            {
                KHDA = new QLKhachSanDataContext();
                KhachHang s = KHDA.KhachHangs.Where(p => p.MaKH == K.MaKH).FirstOrDefault();
                s.MaKH = K.MaKH;
                s.TenKH = K.TenKH;
                s.GioiTinh = K.GioiTinh;
                s.NgaySinh = K.NgaySinh;
                s.CMND = K.CMND;
                s.NgayTao = K.NgayTao;
                s.DiaChi = K.DiaChi;
                s.LoaiKhach = K.LoaiKhach;
                KHDA.SubmitChanges();
                return true;

            }
            catch
            {
                return false;
            }
            finally
            {
                KHDA.Dispose();
            }
        }
        public bool XoaKhachHang(string ma)
        {
            try
            {
                KHDA = new QLKhachSanDataContext();
                KhachHang s = KHDA.KhachHangs.Where(p => p.MaKH == ma).FirstOrDefault();
                KHDA.KhachHangs.DeleteOnSubmit(s);
                KHDA.SubmitChanges();
                return true;

            }
            catch
            {
                return false;
            }
            finally
            {
                KHDA.Dispose();
            }
        }
        public string LayTenKHTheoMa(string ma)
        {
            try
            {
                KHDA = new QLKhachSanDataContext();
                return KHDA.KhachHangs.Where(p => p.MaKH == ma).Select(p => p.TenKH).FirstOrDefault();
            }
            catch
            {
                return null;
            }
            finally
            {
                KHDA.Dispose();
            }
        }
        public string LayLoaiKHTheoMa(string ma)
        {
            try
            {
                KHDA = new QLKhachSanDataContext();
                return KHDA.KhachHangs.Where(p => p.MaKH == ma).Select(p => p.LoaiKhach).FirstOrDefault();
            }
            catch
            {
                return null;
            }
            finally
            {
                KHDA.Dispose();
            }
        }
    }
}
